package com.ucvisitors.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import com.example.ucvisitors.R;
import com.ucvisitors.dao.BuildingDAOStub;
import com.ucvisitors.dao.IBuildingDAO;
import com.ucvisitors.dto.Building;

public class SearchScreen extends Activity {

	//create a variable for the interface
		private IBuildingDAO buildingDAO;
		private String name;
		private double lat;
		private double lon;
		private ArrayAdapter<String> aaBuildingNames;
		private List<Building> buildings = new ArrayList<Building>();
		private AutoCompleteTextView actBuildingName;
		

		@Override
		    protected void onCreate(Bundle savedInstanceState) {
		        super.onCreate(savedInstanceState);
		        setContentView(R.layout.search_screen);	        
		        
		        //link up the text view to a variable
		        actBuildingName = (AutoCompleteTextView) findViewById(R.id.autoComplete);
		        
		        
		        //set the interface to an instance of the stub class
		        buildingDAO = new BuildingDAOStub();
		        
		        try {
					//get a list of building names.
					buildings = buildingDAO.fetchBuildings();
					List<String> buildingNames = new ArrayList<String>();
					int buildingCount = buildings.size();
					
					if (buildingCount != 0)
					{
						for (int i =0; i < buildingCount ; i++)
						{
							Building building = new Building();
							building = buildings.get(i);
							String buildingName = building.getName();
							buildingNames.add(buildingName);
						}
					}
					
					//create an ArrayAdapter
					aaBuildingNames = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, buildingNames);
					
					//link up the adapter and the ac text view
					actBuildingName.setAdapter(aaBuildingNames);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
		    }
		
	
		
		public void onClick_Destination(View v)
		{
			String name = actBuildingName.getText().toString();
			
			for (int j = 0; j < buildings.size();j++)
			{
				Building building = buildings.get(j);
				if (building.getName().compareTo(name) == 0)
				{
					lat = building.getLat();
					lon = building.getLon();
				}
			}
			
			Intent i = new Intent(SearchScreen.this, MainActivity.class);
			i.putExtra("name", name);
			i.putExtra("lon", lon);
			i.putExtra("lat", lat);
	    	startActivity(i);
	    	this.finish();
		}
}
